package com.bbb.blogservice.controller;

import com.bbb.blogservice.entity.BlogContent;
import com.bbb.blogservice.entity.BlogInfo;
import com.bbb.blogservice.entity.BlogTopic;
import com.bbb.blogservice.entity.BlogVideo;
import com.bbb.blogservice.service.blog.BlogService;
import com.bbb.blogservice.service.common.CommonService;
import com.bbb.blogservice.service.user.UserCenterService;
import com.bbb.blogservice.util.ConstantValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "blog")
public class BlogController {

    private final static Logger logger = LoggerFactory.getLogger(BlogController.class);

    @Autowired
    UserCenterService userCenterService;

    @Autowired
    BlogService blogService;

    @Autowired
    CommonService commonService;

    @RequestMapping(value = "saveTopic", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveTopic(BlogTopic blogTopic) {
        Map<String, Object> result = new HashMap<>();
        Integer code;
        String msg;

        try {
            Long topicId = commonService.getGenerateId(ConstantValue.Blog.BLOG_TOPIC);
            blogTopic.setId(topicId);
            blogTopic.setCreateTime(new Date());
            // 此处需以后更改 TODO
            blogTopic.setCreateUser(0L);
            blogService.saveTopic(blogTopic);
            code = 200;
            msg = "ok";
        } catch (Exception e) {

            code = 500;
            msg = "fail";
            logger.error("blogTopic:fail===" + e.getMessage());
            e.printStackTrace();
        }

        result.put("code", code);
        result.put("msg", msg);
        return result;
    }

    @RequestMapping(value = "/updateTopic", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateTopic(BlogTopic blogTopic) {
        Map<String,Object> result = new HashMap<>();
        Integer code;
        String msg;
        try {
            blogService.updateTopic(blogTopic);
            code = 200;
            msg = "ok";
        } catch (Exception e) {
            code = 500;
            msg = "fail";
            logger.error("updateTopic:fail===" + e.getMessage());
            e.printStackTrace();
        }
        result.put("code", code);
        result.put("msg", msg);
        return result;
    }

    @RequestMapping(value = "/createBlog", method = RequestMethod.POST)
    public Map<String, Object> createBlog(BlogInfo blogInfo, String token) throws Exception {
        Map<String,Object> result = new HashMap<>();
        Integer code;
        String msg;
        Long blogInfoId;
        try {
            blogInfoId = commonService.getGenerateId(ConstantValue.Blog.BLOG_INFO);
            blogInfo.setId(blogInfoId);
            blogInfo.setCreateUser(0L);
            BlogContent blogContent = new BlogContent();
            blogContent.setBlogId(blogInfoId);
            blogContent.setCreateUser(0L);
            blogContent.setContent("");
            blogService.createBlog(blogInfo, blogContent);
            code = 200;
            msg = "ok";
        } catch (Exception e) {
            code = 500;
            msg = "fail";
            blogInfoId = null;
            logger.error("createBlog:fail===" + e.getMessage());
            e.printStackTrace();
        }
        result.put("code", code);
        result.put("msg", msg);
        result.put("id", blogInfoId);
        return result;

    }

    @RequestMapping(value = "/saveBlog", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveBlog(BlogContent blogContent, String token) {
        Map<String,Object> result = new HashMap<>();
        Integer code;
        String msg;

        try {
            BlogInfo blogInfo = new BlogInfo();
            blogInfo.setId(blogContent.getBlogId());
            blogInfo.setTitle(blogContent.getTitle());
            blogService.saveBlog(blogContent, blogInfo);
            code = 200;
            msg = "ok";
        } catch (Exception e) {
            code = 500;
            msg = "fail";
            logger.error("msg:fail===" + e.getMessage());
            e.printStackTrace();
        }

        result.put("code", code);
        result.put("msg", msg);
        return result;
    }

    @RequestMapping(value = "getAllTopicByUser", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getAllTopicByUser(String token) {
        Map<String, Object> result = new HashMap<>();
        Integer code;
        String msg;
        List<BlogTopic> blogTopics;
        try {

            blogTopics = blogService.getAllTopicByUser(0L);
            code = 200;
            msg = "ok";
        } catch (Exception e) {
            blogTopics = null;
            code = 500;
            msg = "fail";
            logger.error("getAllTopicByUser:fail===" + e.getMessage());
            e.printStackTrace();
        }
        result.put("code", code);
        result.put("msg", msg);
        result.put("dataList", blogTopics);
        return result;
    }

    @RequestMapping(value = "getAllBlogByTopicId", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getAllBlogByTopicId(String topicId) {
        Map<String, Object> result = new HashMap<>();
        Integer code;
        String msg;
        List<BlogInfo> blogInfoList;
        try {
            blogInfoList = blogService.getAllBlogByTopicId(topicId);
            code = 200;
            msg = "ok";
        } catch (Exception e) {
            code = 500;
            msg = "fail" ;
            blogInfoList = null;
            logger.error("getAllBlogByTopicId:fail===" + e.getMessage());
        }

        result.put("code", code);
        result.put("msg", msg);
        result.put("dataList", blogInfoList);
        return  result;
    }

    @RequestMapping(value = "/getBlogById", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getBlogById(Long id) {
        Map<String,Object> result = new HashMap<>();
        Integer code;
        String msg;
        Map<String, Object> blogContent;

        try {
            blogContent = blogService.getBlogById(id);
            code = 200;
            msg = "ok";

        } catch (Exception e) {
            blogContent = null;
            code = 500;
            msg = "fail";
            logger.error("getBlogById:fail===" + e.getMessage());
        }
        result.put("result", blogContent);
        result.put("code", code);
        result.put("msg", msg);

        return result;
    }

    @RequestMapping(value = "/getAllBlog", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getPageBlog() {
        Map<String,Object> result = new HashMap<>();
        Integer code;
        String msg;
        List<BlogInfo> blogInfoList;
        try {
            Map<String,Object> paramMap = new HashMap<>();
            blogInfoList = blogService.getAllBlog();
            code = 200;
            msg = "ok";
        } catch (Exception e) {
            code = 500;
            msg = "fail";
            blogInfoList = null;
            logger.error("getPageBlog:fail===" + e.getMessage());
        }
        result.put("code", code);
        result.put("msg", msg);
        result.put("dataList", blogInfoList);
        return result;
    }

    @RequestMapping(value = "getBlogVideo", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getBlogVideo(Long blogId) {
        Map<String,Object> result = new HashMap<>();
        Integer code;
        String msg;
        List<BlogVideo> blogVideoList;

        try {
            blogVideoList = blogService.getBlogVideo(blogId);
            code = 200;
            msg = "ok";
        } catch (Exception e) {
            code = 500;
            msg = "fail";
            blogVideoList = null;
            logger.error("getBlogVideo:fail===" + e.getMessage());
            e.printStackTrace();
        }
        result.put("code", code);
        result.put("msg", msg);
        result.put("dataList", blogVideoList);
        return result;
    }
}
